package com.xwd.servlet;

import com.alibaba.fastjson.JSON;
import com.xwd.pojo.Book;
import com.xwd.pojo.PageBean;
import com.xwd.service.BookService;
import com.xwd.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BookServlet
 * @Description: com.xwd.servlet
 * @Auther: xiwd
 * @Date: 2022/2/13 - 02 - 13 - 13:10
 * @version: 1.0
 */
@WebServlet(
        name = "bookservlet",
        urlPatterns = {
                "/bookservlet.do"
        }
)
public class BookServlet extends HttpServlet {
    //properties
    private BookService bookService=new BookServiceImpl();
    //setter

    //getter

    //constructors

    //methods
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //super.doPost(request,response);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("Utf-8");
        response.setContentType("application/json;charset=UTF-8");
        PageBean<Book> pageBean=null;
        //writer对象
        PrintWriter writer = response.getWriter();
        //解析请求参数
        Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String bookname = request.getParameter("bookname");
        //调用service作业务逻辑处理
        pageBean = bookService.queryForPage(currentPage, pageSize);
        //将数据放入请求域
        request.setAttribute("pagebean",pageBean);
        System.out.println(request.getAttribute("pagebean").toString());
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("pageSize",pageSize);
        //响应数据->页面跳转
        request.getRequestDispatcher("bookinfo.jsp").forward(request,response);


    }
}
